package com.pzhu.service.impl;

import com.pzhu.dao.BrandDao;
import com.pzhu.domain.Brand;
import com.pzhu.service.BrandService;
import com.pzhu.util.PageBean;
import com.pzhu.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;

public class BrandServiceImpl implements BrandService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
    public List<Brand> selectAll() {
        //2.SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);

        List<Brand> brands = brandDao.selectAll();
        sqlSession.close();
        return brands;
    }

    public void add(Brand brand)  {
        //2.SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        brandDao.add(brand);
        //手动提交
        sqlSession.commit();
        sqlSession.close();
    }

    public void update(Brand brand) {
        //2.SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        brandDao.update(brand);
        //手动提交
        sqlSession.commit();
        sqlSession.close();
    }

    public void delete(int id) {
        //2.SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        brandDao.delete(id);
        //手动提交
        sqlSession.commit();
        sqlSession.close();
    }

    public void deleteByIds(int[] ids) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        brandDao.deleteByIds(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);

        //计算开始索引
        int begin = (currentPage -1) * pageSize;
        //查询的条目数（总数）
        int size = pageSize;

        //查询当前页数据
        List<Brand> rows = brandDao.selectByPage(begin, size);
        //查询总记录数
        int totalCount = brandDao.selectTotalCount();

        //封装成PageBean对象
        PageBean<Brand> PageBean = new PageBean<>();
        PageBean.setRows(rows);
        PageBean.setTotalCount(totalCount);
        //释放资源
        sqlSession.close();
        return PageBean;

    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);

        //计算开始索引
        int begin = (currentPage -1) * pageSize;
        //查询的条目数（总数）
        int size = pageSize;

        //处理brand条件 模糊表达式
        String brandName = brand.getBrandName();
        if(brandName != null && brandName.length() > 0){
            brand.setBrandName("%" +brandName +  "%");
        }

        String CompanyName = brand.getCompanyName();
        if(CompanyName != null && CompanyName.length() > 0){
            brand.setCompanyName("%" +CompanyName +  "%");
        }

        //查询当前页数据
        List<Brand> rows = brandDao.selectByPageAndCondition(begin, size, brand);

        //查询总记录数
        int totalCount = brandDao.selectTotalCountByCondition(brand);

        //封装成PageBean对象
        PageBean<Brand> PageBean = new PageBean<>();
        PageBean.setRows(rows);
        PageBean.setTotalCount(totalCount);

        //释放资源
        sqlSession.close();

        return PageBean;

    }
}
